@echo off
chcp 65001
set "scriptDir=%~dp0"
cd /d "%scriptDir%"
cd lexanalyze
if exist "lexanalyze.exe" (
    powershell -Command "Write-Host '删除lexanalyze.exe...' -ForegroundColor Yellow"
    del "lexanalyze.exe"
)
gcc.exe -o lexanalyze.exe lexanalyze.c -L"%scriptDir%\lexanalyze\build" -llexanalyze -L"%scriptDir%\stl\build" -lstl
powershell -Command "Write-Host '■ 词法分析器生成成功...' -ForegroundColor Green"
cd ../grammar
if exist "ll1.exe" (
    powershell -Command "Write-Host '删除ll1.exe...' -ForegroundColor Yellow"
    del "ll1.exe"
)
if exist "opg.exe" (
    powershell -Command "Write-Host '删除opg.exe...' -ForegroundColor Yellow"
    del "opg.exe"
)
if exist "lr0.exe" (
    powershell -Command "Write-Host '删除lr0.exe...' -ForegroundColor Yellow"
    del "lr0.exe"
)
gcc.exe -o ll1.exe ll1.c -L"%scriptDir%\grammar\build" -lparsing -L"%scriptDir%\stl\build" -lstl
powershell -Command "Write-Host '■ 语法分析器（LL1）生成成功...' -ForegroundColor Green"
gcc.exe -o opg.exe opg.c -L"%scriptDir%\grammar\build" -lparsing -L"%scriptDir%\stl\build" -lstl
powershell -Command "Write-Host '■ 语法分析器（OPG）生成成功...' -ForegroundColor Green"
gcc.exe -o lr0.exe lr0.c -L"%scriptDir%\grammar\build" -lparsing -L"%scriptDir%\stl\build" -lstl
powershell -Command "Write-Host '■ 语法分析器（LR0）生成成功...' -ForegroundColor Green"

cd ../IntermediateCodeGenerator
if exist build (
    powershell -Command "Write-Host '删除旧build目录...' -ForegroundColor Yellow"
    rd /s /q build
)
mkdir build && cd build
cmake -G "MinGW Makefiles" ..  && mingw32-make
cd ..
if exist "icg.exe" (
    powershell -Command "Write-Host '删除icg.exe...' -ForegroundColor Yellow"
    del "icg.exe"
)
gcc.exe -o icg.exe icg.c -L"%scriptDir%\IntermediateCodeGenerator\build" -licg 
powershell -Command "Write-Host '■ 中间代码生成器生成成功...' -ForegroundColor Green"
pause